package com.czh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czh.constants.SystemConstants;
import com.czh.domain.ResponseResult;
import com.czh.domain.dto.QueryCategoryDto;
import com.czh.domain.entity.Article;
import com.czh.domain.entity.Category;
import com.czh.domain.vo.CategoryVo;
import com.czh.domain.vo.CategoryPageVo;
import com.czh.mapper.CategoryMapper;
import com.czh.service.ArticleService;
import com.czh.service.CategoryService;
import com.czh.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2024-03-19 17:47:59
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private ArticleService articleService;

    /**
     * 前端展示已发布的文章的分类列表
     *
     * @return ResponseResult
     */
    @Override
    public ResponseResult<List<CategoryVo>> getCategoryList() {
        //查询文章表  状态为已发布的文章
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(articleWrapper);
        //获取文章的分类id，并且去重
        Set<Long> categoryIds = articleList.stream().map(Article::getCategoryId).collect(Collectors.toSet());
        /**Set<Long> categoryIds = new HashSet<>();
         *                   for (Article article : articleList) {
         *                       Long categoryId = article.getCategoryId();
         *                       categoryIds.add(categoryId);
         *                   }
         */
        //查询分类表
        List<Category> categories = listByIds(categoryIds);
        categories = categories.stream()
                .filter(category -> SystemConstants.CATEGORY_STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        List<CategoryVo> categoryPageVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryPageVos);
    }
    /**
     * 后端写博文页面展示分类下拉菜单
     *
     * @return ResponseResult
     */
    @Override
    public ResponseResult<List<CategoryVo>> listAllCategory() {
        LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
        categoryWrapper.eq(Category::getStatus,SystemConstants.CATEGORY_STATUS_NORMAL).
                eq(Category::getDelFlag,SystemConstants.DEL_FLAG);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list(categoryWrapper), CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    /**
     * 后端分类管理列表展示
     *
     * @param pageNum          页码
     * @param pageSize         每页显示条数
     * @param queryCategoryDto 接收
     */
    @Override
    public ResponseResult<CategoryPageVo> getCategoryPageList(Integer pageNum, Integer pageSize, QueryCategoryDto queryCategoryDto) {
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.like(StringUtils.hasText(queryCategoryDto.getName()),Category::getName, queryCategoryDto.getName()).
                eq(StringUtils.hasText(queryCategoryDto.getStatus()),Category::getStatus, queryCategoryDto.getStatus());

        Page<Category> categoryPage = new Page<>(pageNum, pageSize);
        page(categoryPage,categoryLambdaQueryWrapper);
        return ResponseResult.okResult(new CategoryPageVo(categoryPage.getRecords(),categoryPage.getTotal()));
    }

    @Override
    public ResponseResult<Object> addCategory(Category category) {
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Object> deleteCategory(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<CategoryVo> getCategoryById(Long id) {
        CategoryVo categoryVo = BeanCopyUtils.copyBean(getById(id), CategoryVo.class);
       return ResponseResult.okResult(categoryVo);
    }

    @Override
    public ResponseResult updateCategory(Category category) {
        updateById(category);
        return ResponseResult.okResult();
    }
}

